<template>
  <div class="login">
    <div class="login-left">
      <img src="@/assets/images/login/bg_0.png" alt="">
    </div>
    <div class="login-right">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <!-- <h3 class="title">九步法后台管理系统</h3> -->
        <img class="logo-img" src="@/assets/images/logo.png" alt="">
        <el-form-item prop="username" label="登录账号">
          <el-input
            class="login-input"
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入登录账号"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="登录密码">
          <el-input
            class="login-input"
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请再次密码"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled" label="图形验证码">
          <div class="flex w">
            <div style="width: 63%">
              <el-input
                class="login-input"
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                @keyup.enter.native="handleLogin"
              >
              </el-input>
            </div>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </div>
        </el-form-item>
       <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> -->
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            class="login-btn"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
<!--          <div class="register" v-if="register">
            <router-link class="link-type" :to="'/register'">注册账号</router-link>
          </div>-->
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          //this.codeUrl = "data:image/gif;base64," + res.img;
          this.codeUrl = res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="scss">
  .login-form{
    .el-form-item__label{
      font-size: 16px;
      color: #333333;
      font-weight: normal;
    }
  }

  .login-input .el-input__inner{
    height: 60px;
    background-color: #F5F6FA;
    padding:0 15px;
    line-height: 60px;
    border:none;
    border-radius: 10px;
    font-size: 16px;
  }
  .login-input .el-input__inner::-webkit-input-placeholder{
    color:#949494
  }
  .login-input .el-input__inner::-moz-placeholder{
    color:#949494
  }
  .login-input .el-input__inner::-ms-input-placeholder{
    color:#949494
  }
</style>
<style rel="stylesheet/scss" lang="scss" scoped>

.login {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  // background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  .login-left {
    height: 100%;
    img {
      height: 100%;
      // max-width: 40vh;
    }
  }
  .login-right {
    flex: 1; height: 100%; display: flex; align-items: center; justify-content: center;
    padding: 0 20px;
  }
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 10px;
  background: #ffffff;
  box-shadow: 0px 1px 20px 0px rgba(74,121,177,0.1);
  width: 480px;
  padding: 45px;

  // .el-input {
  //   height: 38px;
  //   input {
  //     height: 38px;
  //   }
  // }
  // .input-icon {
  //   height: 39px;
  //   width: 14px;
  //   margin-left: 2px;
  // }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 130px;
  height: 60px;
  margin-left: 10px;
  // float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    width: 100%;
    height: 60px;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

.logo-img{
  width: 374px;
  margin: 0 auto 30px;
}
.login-btn{
  height: 60px;
  background-color: #008FFF;
  border-radius: 30px;
  text-align: center;
  line-height: 60px;
  color: #ffffff;
  padding:0;
}
.register{
  text-align: center;
  margin-top: 20px;
  color:#008FFF
}
.flex {
  display:flex;
}
.w {
  width:100%;
}
</style>
